package com.ruoyi.web.controller.mobile;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EncryptedDataEntity;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.WxLoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WeChatApiUtil;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "登录验证")
@RestController
@RequestMapping("/wx")
public class MobileLoginController
{
    private static final Logger log = LoggerFactory.getLogger(MobileLoginController.class);

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private WeChatApiUtil weChatApiUtil;


    /**
     * 移动端微信登录
     * @param wxLoginBody
     * @return
     */
    @PostMapping("/wxLogin")
    public AjaxResult wxLogin(@RequestBody WxLoginBody wxLoginBody){
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.wxLogin(wxLoginBody.getCode(),wxLoginBody.getEncryptedIv(),wxLoginBody.getEncryptedData(),
                wxLoginBody.getEncryptedDataEntity());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/getPhoneByCode")
    public AjaxResult getPhoneByCode(@RequestBody Map<String,Object> map){
        AjaxResult ajax = AjaxResult.success();
        String accessToken = weChatApiUtil.getAccessToken();
        if (StringUtils.isEmpty(accessToken)){
            log.error("获取accessToken失败->accessToken: [{]]",accessToken);
            return AjaxResult.error("获取accessToken失败->accessToken: [ "+accessToken+"]");
        }
        String code = (String)map.get("code");
        EncryptedDataEntity encryptedDataEntity = weChatApiUtil.getUserPhoneNumber(accessToken, code);
        ajax.put("data", encryptedDataEntity);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
